package ru.edu.vstu.atpp.acm.ui.components.user;


import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import ru.edu.vstu.atpp.acm.ui.security.AuthenticationException;
import ru.edu.vstu.atpp.acm.ui.services.authenticator.Authenticator;

public class UserLoginForm {

    @Parameter
    private Class returnPage;

    @Property
    private String login;

    @Property
    private String password;

    @Inject
    private Authenticator authenticator;

    @Component
    private Form loginForm;

    @Inject
    private Messages messages;


    public boolean getLoggedIn() {
        return authenticator.isLoggedIn();
    }


    @Log
    public Object onSubmitFromLoginForm()
    {
        try
        {
            authenticator.login(login, password);
        }
        catch (AuthenticationException ex)
        {
            loginForm.recordError(messages.get("error.login"));
            return null;
        }

        return this;
    }
}